﻿using UnityEngine;
using System.Collections;
using NetWork;
using UnityEngine.SceneManagement;
using AssetLoad;
using Dict.Blo;
using System.Collections.Generic;

namespace Room
{
    public class RoomUnionFight : RoomBase
    {
        public RoomUnionFightCheckPvp checkPvp;
        public const string PREFIX = "particles/";
        #region 各种效果

        public const string BLOOD_BAR = @"UIcomponent/BloodBar";
        public const string WORD_TIP = @"UIcomponent/WordTip";

        public static string DIE = "";
        public static string ATTACK = "";

        public static string DEFENSE = "";
        public static string LIGHT = "";
        public static string REBORN = "";
        public static string NO_ENEMY = "";
        public static string RECOVER_HP_UI = "";
        #endregion


        #region 抛射物:塔
        public static string ALO_PROJECTILE = "";
        public static string SAO_PROJECTILE = "";
        public static string SAO_TOWER_FIRE = "";
        public static string ALO_PROJECTILE_BEHIT = "";
        public static string SAO_PROJECTILE_BEHIT = "";
        public static string PROJECTILE_BEHIT_MAKE = "";
        #endregion

        #region 战车
        public static string CHARIOT_MOVE = "";
        public static string CHARIOT_DIE = "";
        public static string CHARIOT_STAND = "";
        public static string CHARIOT_SMOKE = "";
        public static string CHARIOT_FIRE = "";
        #endregion

        #region 复活点
        public static string REVIVE_OCCUPY_ED = "";
        public static string REVIVE_OCCUPY_ING = "";
        public static string REVIVE_SAO = "";
        public static string REVIVE_ALO = "";
        #endregion

        #region 战车工厂
        public static string CHARIOT_FACTORY_COMPLETE = "";
        #endregion

        #region 物品使用效果
        public static string RECOVER_HP_SMALL = "";
        public static string RECOVER_HP_BIG = "";
        public static string BOMB_SMALL = "";
        public static string BOMB_BIG = "";
        public static string BOMB_SMALL_HIT = "";
        public static string BOMB_BIG_HIT = "";
        public static string FIX_TOOL = "";
        public static string SIEGE_HAMMER = "";
        public static string RECOVER_MATRIX = "";
        public static string MORALE_UP = "";
        public static string RECOVER_RAIN = "";
        #endregion

        #region 城门
        public static string GATE_HIT = "";
        public static string GATE_DESTROY = "";
        #endregion

        #region UI任务
        public static string Five_Win = "";
        public static string Ten_Win = "";
        public static string Gate_Destroy = "";
        public static string Revenge = "";
        public static string Count_Down = "";
        #endregion

        #region 传送
        public static string TRANSFER_STATIC = "";
        public static string TRANSFER_EFFECT_BEGIN = "";
        public static string TRANSFER_EFFECT_END = "";
        #endregion

      

        List<string> Particles = new List<string>();
        
        void Awake()
        {
            DIE = PREFIX + DictParticleBlo.GetCommonParticle("1").ResourcePath;
            ATTACK = PREFIX + DictParticleBlo.GetCommonParticle("9").ResourcePath;
            LIGHT = PREFIX + DictParticleBlo.GetCommonParticle("11").ResourcePath;
            REBORN = PREFIX + DictParticleBlo.GetCommonParticle("8").ResourcePath;
            NO_ENEMY = PREFIX + DictParticleBlo.GetCommonParticle("14").ResourcePath;
            RECOVER_HP_UI = PREFIX + DictParticleBlo.GetCommonParticle("RecoverHp").ResourcePath;
            DEFENSE = PREFIX + DictParticleBlo.GetCommonParticle("10").ResourcePath;

            ALO_PROJECTILE = PREFIX + DictParticleBlo.GetCommonParticle("ALOProj").ResourcePath;
            SAO_PROJECTILE = PREFIX + DictParticleBlo.GetCommonParticle("SAOProj").ResourcePath;
            SAO_TOWER_FIRE = PREFIX + DictParticleBlo.GetCommonParticle("SAOFire").ResourcePath;
            ALO_PROJECTILE_BEHIT = PREFIX + DictParticleBlo.GetCommonParticle("ALOBeHit").ResourcePath;
            SAO_PROJECTILE_BEHIT = PREFIX + DictParticleBlo.GetCommonParticle("SAOBeHit").ResourcePath;
            PROJECTILE_BEHIT_MAKE = PREFIX + DictParticleBlo.GetCommonParticle("FactoryWork").ResourcePath;
            CHARIOT_MOVE = PREFIX + DictParticleBlo.GetCommonParticle("ChariotRun").ResourcePath;
            CHARIOT_DIE = PREFIX + DictParticleBlo.GetCommonParticle("ChariotDie").ResourcePath;
            CHARIOT_STAND = PREFIX + DictParticleBlo.GetCommonParticle("ChariotStand").ResourcePath;

            CHARIOT_SMOKE = PREFIX + DictParticleBlo.GetCommonParticle("ChariotSmoke").ResourcePath;
            CHARIOT_FIRE = PREFIX + DictParticleBlo.GetCommonParticle("ChariotFire").ResourcePath;

            CHARIOT_FACTORY_COMPLETE = PREFIX + DictParticleBlo.GetCommonParticle("FactoryComplete").ResourcePath;
            REVIVE_OCCUPY_ED = PREFIX + DictParticleBlo.GetCommonParticle("Revived").ResourcePath;
            REVIVE_OCCUPY_ING = PREFIX + DictParticleBlo.GetCommonParticle("Reviving").ResourcePath;

            RECOVER_HP_SMALL = PREFIX + DictParticleBlo.GetCommonParticle("recoverSmall").ResourcePath;
            RECOVER_HP_BIG = PREFIX + DictParticleBlo.GetCommonParticle("recoverBig").ResourcePath;
            BOMB_SMALL = PREFIX + DictParticleBlo.GetCommonParticle("bombSmall").ResourcePath;
            BOMB_BIG = PREFIX + DictParticleBlo.GetCommonParticle("bombBig").ResourcePath;
            BOMB_SMALL_HIT = PREFIX + DictParticleBlo.GetCommonParticle("bombSmallHit").ResourcePath;
            BOMB_BIG_HIT = PREFIX + DictParticleBlo.GetCommonParticle("bombBigHit").ResourcePath;
            FIX_TOOL = PREFIX + DictParticleBlo.GetCommonParticle("fixTool").ResourcePath;
            SIEGE_HAMMER = PREFIX + DictParticleBlo.GetCommonParticle("siegeHammer").ResourcePath;
            RECOVER_MATRIX = PREFIX + DictParticleBlo.GetCommonParticle("recoverMatrix").ResourcePath;
            MORALE_UP = PREFIX + DictParticleBlo.GetCommonParticle("moraleUp").ResourcePath;
            RECOVER_RAIN = PREFIX + DictParticleBlo.GetCommonParticle("recoverRain").ResourcePath;

            GATE_HIT = PREFIX + DictParticleBlo.GetCommonParticle("gate_hit").ResourcePath;
            GATE_DESTROY = PREFIX + DictParticleBlo.GetCommonParticle("gate_destroy").ResourcePath;

            Five_Win = PREFIX + DictParticleBlo.GetCommonParticle("FiveLianSheng").ResourcePath;
            Ten_Win = PREFIX + DictParticleBlo.GetCommonParticle("TenLianSheng").ResourcePath;
            Gate_Destroy = PREFIX + DictParticleBlo.GetCommonParticle("Jipochengmen").ResourcePath;
            Revenge = PREFIX + DictParticleBlo.GetCommonParticle("FuChou").ResourcePath;
            Count_Down = PREFIX + DictParticleBlo.GetCommonParticle("CountDown").ResourcePath;

            TRANSFER_EFFECT_BEGIN = PREFIX + DictParticleBlo.GetCommonParticle("Chuansongzhen1").ResourcePath;
            TRANSFER_EFFECT_END = PREFIX + DictParticleBlo.GetCommonParticle("Chuansongzhen2").ResourcePath;

            TRANSFER_STATIC = PREFIX + DictParticleBlo.GetCommonParticle("TransferStatic").ResourcePath;

            REVIVE_SAO = PREFIX + DictParticleBlo.GetCommonParticle("ReviveSaoFlag").ResourcePath;
            REVIVE_ALO = PREFIX + DictParticleBlo.GetCommonParticle("ReviveAloFlag").ResourcePath;



            Particles.Add(DIE);
            Particles.Add(ATTACK);
            Particles.Add(LIGHT);
            Particles.Add(REBORN);
            Particles.Add(NO_ENEMY);
            Particles.Add(RECOVER_HP_UI);
            Particles.Add(DEFENSE);

            Particles.Add(ALO_PROJECTILE);
            Particles.Add(SAO_PROJECTILE);
            Particles.Add(SAO_TOWER_FIRE);
            Particles.Add(ALO_PROJECTILE_BEHIT);
            Particles.Add(SAO_PROJECTILE_BEHIT);
            Particles.Add(PROJECTILE_BEHIT_MAKE);
            Particles.Add(CHARIOT_MOVE);
            Particles.Add(CHARIOT_DIE);
            Particles.Add(CHARIOT_SMOKE);
            Particles.Add(CHARIOT_FIRE);
            Particles.Add(CHARIOT_STAND);
            Particles.Add(CHARIOT_FACTORY_COMPLETE);
            Particles.Add(REVIVE_OCCUPY_ED);
            Particles.Add(REVIVE_OCCUPY_ING);
            Particles.Add(REVIVE_SAO);
            Particles.Add(REVIVE_ALO);
            
            Particles.Add(RECOVER_HP_SMALL);
            Particles.Add(RECOVER_HP_BIG);
            Particles.Add(BOMB_SMALL);
            Particles.Add(BOMB_BIG);
            Particles.Add(BOMB_SMALL_HIT);
            Particles.Add(BOMB_BIG_HIT);
            Particles.Add(FIX_TOOL);
            Particles.Add(SIEGE_HAMMER);
            Particles.Add(RECOVER_MATRIX);
            Particles.Add(MORALE_UP);
            Particles.Add(RECOVER_RAIN);

            Particles.Add(GATE_DESTROY);
            Particles.Add(GATE_HIT);

            Particles.Add(Five_Win);
            Particles.Add(Ten_Win);
            Particles.Add(Gate_Destroy);
            Particles.Add(Revenge);
            Particles.Add(Count_Down);

            Particles.Add(TRANSFER_EFFECT_BEGIN);
            Particles.Add(TRANSFER_EFFECT_END);
            Particles.Add(TRANSFER_STATIC);
        }
        


        public override ServerType GetServerType()
        {
            return ServerType.unionFight;
        }

        public override SceneType GetSceneType()
        {
            return SceneType.UNIONFIGHT;
        }

        protected override void OnLeave()
        {
            base.OnLeave();
            netManager.Leave();

            checkPvp.Leave();

            smallMap.HideMiniMap();
            smallMap.Unload();
            charactorManager.ClearAll();
            UnRegistParticle();
            inited = false;
        }

        protected override void OnEnter(params object[] param)
        {
            base.OnEnter(param);
            if(!inited)
            {
                Init();
            }
            checkPvp.Enter();
        }
        
        public override void Init()
        {
            base.Init();
            checkPvp = new RoomUnionFightCheckPvp();
            checkPvp.Init(this);

            StartCoroutine(_WaitForLoadComplete());
            inited = true;
        }
        

        /// <summary>
        /// 加载固定传输点
        /// </summary>
        void LoadFixedTransferPoint()
        {
            var transfers = Dict.Blo.DictUnionFightTransferBlo.GetAll();
            foreach(var transfer in transfers)
            {
                if(transfer.TransferType == 0)
                {
                    charactorManager.CreateUnionTransfer(transfer);
                }
            }
        }

        IEnumerator _WaitForLoadComplete()
        {
            if(SceneManager.GetActiveScene().name!=SceneName.unionFight)
            {
                var scenePath = SceneName.sceneFolder + SceneName.unionFight;
                yield return StartCoroutine(AssetBundleManager.Instance.LoadSceneAsync(scenePath, SceneName.unionFight));
            }

            yield return StartCoroutine(_RegistParticle());

            LoadFixedTransferPoint();
            OnLoadComplete();
        }

        protected void OnLoadComplete()
        {
            netManager.Enter();
            smallMap.ShowMiniMap();
            smallMap.SetData(Dict.Blo.TableSceneBlo.GetSceneById(SceneName.unionFight));
            
            sceneCamera.Set(cameraDelta, new Vector3(cameraAngle, 0, 0), cameraFOV);
            StaticData.cameraRotate = Vector3.zero;
            CameraTool.Instance.ResetSceneBloomEffect();

            CreateNpc();
        }


        void CreateNpc()
        {
            var npcs = Dict.Blo.DictUnionFightNpcBlo.GetAll();
            foreach(var npc in npcs)
            {
                charactorManager.CreateUnionNpc(npc);
            }
        }

        protected override void Tick()
        {
            netManager.Tick();
            smallMap.Tick();
            viewController.Tick();
            charactorManager.Tick();
            checkPvp.Tick();
            sceneCamera.Set(cameraDelta, new Vector3(cameraAngle, 0, 0), cameraFOV);
        }

        public override void InitRoomNet()
        {
            netManager = new RoomNetUnionFight();
            netManager.Init(this);
        }
        
        protected override void OnBackground()
        {
            base.OnBackground();
            netManager.Pause();
            viewController.Pause();
            uiManager.gameObject.SetActive(false);
            EasyTouchTool.Instance.Hide();
        }

        protected override void OnForeground()
        {
            base.OnForeground();
            netManager.Resume();
            viewController.Resume();
            uiManager.gameObject.SetActive(true);
            EasyTouchTool.Instance.ShowJoystick();
        }

        public override void OnMainCharactorLoaded(Transform mc)
        {
            base.OnMainCharactorLoaded(mc);
            viewController.SetPlayer(mc);

            checkPvp.OnMainCharactorLoaded();
            smallMap.miniMapComponent.SetMajorCharactor(mc);
            LoadingPanel.Instance.HideLoading();
            EasyTouchTool.Instance.ShowJoystick();
        }

        protected override void OnCharactorDestroy(RoomCharactor rc)
        {
            base.OnCharactorDestroy(rc);
            var uuid = rc.name;
            var net = netManager as RoomNetUnionFight;
            net.OnCharactorDestroy(uuid);
            checkPvp.OnUnloadCompleteCharactor(uuid);
        }

        protected override void OnNewCharactorCreate(RoomCharactor rc)
        {
            if (   rc.type == CharactorType.UnionFightChariotFactory
                || rc.type == CharactorType.UnionFightRevivePoint
                || rc.type == CharactorType.UnionFightTower
                || rc.type == CharactorType.UnionFightGate)
            {
                smallMap.miniMapComponent.LoadFixedCharactor(rc.name, rc);
            }
            else
            {
                smallMap.miniMapComponent.LoadLiveCharactor(rc.name, rc);
            }

            checkPvp.OnLoadCompleteCharactor(rc);
        }

        IEnumerator _RegistParticle()
        {
            AssetLoad.ResourceManager.Instance.LoadResource(BLOOD_BAR);//血条
            GameObjectPoolManager.Instance.Regist(BLOOD_BAR);

            AssetLoad.ResourceManager.Instance.LoadResource(WORD_TIP);//崩字
            GameObjectPoolManager.Instance.Regist(WORD_TIP);

            

            foreach (var parti in Particles)
            {
                yield return RegistAssets(parti);
            }
        }

        IEnumerator RegistAssets(string str)
        {
            AssetBundleManager.Instance.LoadAssetBundle(str);
            while (!AssetBundleManager.Instance.IsLoadedAssetBundle(str))
            {
                yield return null;
            }
            GameObjectPoolManager.Instance.Regist(str, CommonMethod.GetAssetNameFromAssetBundleName(str));
        }


        void UnRegistParticle()
        {
            foreach(var parti in Particles)
            {
                Unregiset(parti);
            }

            GameObjectPoolManager.Instance.UnRegist(BLOOD_BAR);
            GameObjectPoolManager.Instance.UnRegist(WORD_TIP);
        }

        void Unregiset(string str)
        {
            GameObjectPoolManager.Instance.UnRegist(str, CommonMethod.GetAssetNameFromAssetBundleName(str));
        }

#if UNITY_EDITOR
        public ProtoBuf.Message.SCUFElementMove moveMsg;
        public void OnDrawGizmos()
        {
            if(moveMsg == null)
            {
                return;
            }
            foreach (var p in moveMsg.pos)
            {
                Gizmos.color = Color.red;
                Gizmos.DrawSphere(new Vector3(p.x, p.y, p.z), 1f);
            }
        }
#endif
    }
}